package org.data12.SpringbootDemo.controller;

import org.data12.SpringbootDemo.config.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;

@Controller
public class HelloController {

    //依赖注入
    @Autowired
    private RedisConfig redisConfig;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }

    @RequestMapping("/index")
    public String index(Model model){

        String msg="hello,Tangshan!";
        model.addAttribute("msg",msg);
        //传递单个数据
        int age=18;
        model.addAttribute("age",age);
        //传递数组
        ArrayList<String> arrayList=new ArrayList<>();
        arrayList.add("石家庄");
        arrayList.add("廊坊");
        arrayList.add("保定");
        arrayList.add("哈尔滨");
        arrayList.add("石家庄");
        arrayList.add("热部署");
        model.addAttribute("lists",arrayList);
        return "index";
    }

    @RequestMapping("/redis")
    @ResponseBody
    public RedisConfig redis(){
        return redisConfig;
    }

    @RequestMapping("/getDemoHtml")
    public String getDemoHtml(Model model){
        //此处是需要展示的html在templates下的具体路径
        HashMap<String, Object> reMap = new HashMap<>();
        reMap.put("userName", "张三");
        reMap.put("userAge", 20);
        model.addAllAttributes(reMap);
        return "demo/demoHtm";
    }
}
